package com.changhongit.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.changhongit.entity.ServiceProject;
import com.changhongit.entity.ServiceProvider;
import com.changhongit.entity.User;
import com.changhongit.service.ServiceProjectService;
import com.changhongit.service.ServiceProviderService;
import com.changhongit.util.Common;
import com.changhongit.util.PageView;

/**
 * 
 * @author JacksonYang 2014-2-13
 * @Email:
 * @version 1.0v
 */
@Controller
@RequestMapping(value = "/background/serviceProvider/project")
public class ServiceProjectController {
	
	@Autowired
	private ServiceProjectService serviceProjectService;
	
	@Autowired
	private ServiceProviderService serviceProviderService;
	
	/**
	 * 跳到新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "addUI")
	public String addUI(Model model) {
		return "/background/serviceProvider/project/add";
	}

	/**
	 * 保存新增
	 * 
	 * @param model
	 * @param resources
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(Model model, ServiceProject serviceProject,HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("userSession");
		ServiceProvider serviceProvider = serviceProviderService.findOneServiceProvider(user.getUserName());
		serviceProject.setSproviderid(serviceProvider.getId());
		serviceProject.setTime(new Date());
		serviceProjectService.add(serviceProject);
		return "redirect:query.html";
	}

	/**
	 * 分页查询
	 * 
	 * @param model
	 * @param resources
	 * @param pageNow
	 * @return
	 */
	@RequestMapping(value = "query")
	public String query(Model model, ServiceProject serviceProject, String pageNow,HttpServletRequest request) {
		PageView pageView = null;
		User user = (User) request.getSession().getAttribute("userSession");
		String loginType = (String) request.getSession().getAttribute("loginType");
		if(loginType.equals("serviceProvider")){
			ServiceProvider serviceProvider = serviceProviderService.findOneServiceProvider(user.getUserName());
			serviceProject.setSproviderid(serviceProvider.getId());
		}
		if (Common.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		pageView = serviceProjectService.query(pageView, serviceProject);
		model.addAttribute("pageView", pageView);
		return "/background/serviceProvider/project/list";
	}

	/**
	 * 根据id删除
	 * 
	 * @param model
	 * @param resourcesId
	 * @return
	 */
	@RequestMapping(value = "deleteById")
	public String deleteById(Model model, String id) {
		serviceProjectService.delete(id);
		return "redirect:query.html";
	}

	/**
	 * 查询单条记录
	 * 
	 * @param model
	 * @param resourcesId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "getById")
	public String getById(Model model, String id, int typeKey) {
		ServiceProject serviceProject = serviceProjectService.getById(id);
		model.addAttribute("serviceProject", serviceProject);
		if (typeKey == 1) {
			return "/background/serviceProvider/project/edit";
		} else {
			return "/background/serviceProvider/project/show";
		}
	}

	/**
	 * 更新修改的信息
	 * 
	 * @param model
	 * @param resources
	 * @return
	 */
	@RequestMapping(value = "update")
	public String updateResources(Model model, ServiceProject serviceProject) {
		serviceProjectService.modify(serviceProject);
		return "redirect:query.html";
	}

	@RequestMapping(value = "deleteAll")
	public String deleteAll(String[] check, Model model) {
		for (String id : check) {
			serviceProjectService.delete(id);
		}
		return "redirect:query.html";
	}
}
